
require('./style.css')
module.exports = require('angular')
    .module('components.login', [
        require(__SERVICES__ + '/efunfun/login').name,
        require(__COMPONENTS__ + '/server/service').name
    ])
    .directive('components.login', function (login) {
        return {
            scope: {},
            replace: true,
            template: require('./template.html'),
            link: function (scope, ele, attr) {
                scope.selectSid = 0
                scope.exchangeIsShow = false
                scope.customiseFun = ''
                scope.customisePermit = false;
                scope.rolePermit = false
                scope.fun = 0
                scope.roleFlag = true
                scope.warnTxt = '請輸入您要充值的Fun點'
                scope.isLogin = false
                scope.roleTxt = '尚未創建角色'
                scope.funPointArr = [50, 150, 500, 1000, 3000, 5000, 10000]
                login.getUserData().success(function (data, status, headers, config) {
                    data != null && data.ID && (scope.isLogin = true) && (scope.userData = data)
                })
            },
            controller: 'login'
        }
    })
    .controller('login', function (serverservice, $scope, login) {
        serverservice.__defineSetter__('serverList', function (serverList) {
            $scope.serverList = serverList
        })
        $scope.toLogin = function () {
            login.userLogin({
                uname: $scope.uname,
                password: $scope.password
            }).success(function (data, status, headers, config) {
                (data.result != 1) && alert(data.message) || location.reload()
            })
        }
        $scope.toLogout = function () {
            login.userLogout().success(function (data, status, headers, config) {
                (data.result === 1) && (location.href = data.forward)
            })
        }
        $scope.funValuate = function (fun, $event) {
            var e = angular.element($event.target)
            e.parent().children().removeClass('focus')
            e.addClass('focus')
            $scope.fun = fun;
            if ($scope.customiseFun.length === 0) {
                $scope.warnTxt = ''
                $scope.customisePermit = true
            }
        }
        $scope.examFunPoint = function () {
            if ((/^\d+$/).test($scope.customiseFun)) { // 为数字
                if ($scope.customiseFun < 10) {
                    $scope.warnTxt = '不能小於10'
                    $scope.customisePermit = false
                } else if ($scope.customiseFun % 10 != 0) {
                    $scope.warnTxt = '只限10的倍數'
                    $scope.customisePermit = false
                } else {
                    $scope.warnTxt = ''
                    $scope.customisePermit = true
                }
            } else {
                if ($scope.customiseFun.length === 0) {
                    if ($scope.fun === 0) {
                        $scope.warnTxt = '請輸入您要充值的Fun點'
                        $scope.customisePermit = false
                    } else {
                        $scope.warnTxt = ''
                        $scope.customisePermit = true
                    }
                } else {
                    $scope.warnTxt = '請輸入整數點'
                    $scope.customisePermit = false
                }
            }
        }
        $scope.checkRole = function () {
            $scope.roleFlag = false
            login.checkRole($scope.selectSid).success(function (data) {
                $scope.roleFlag = true
                if (data.status) {
                    $scope.rolePermit = true
                } else {
                    $scope.rolePermit = false
                }
                $scope.roleTxt = data.msg
            })
        }
        $scope.exchangeSubmit = function () {
            if ($scope.rolePermit && $scope.roleFlag) {
                if ($scope.customisePermit) {
                    if ($scope.customiseFun.length === 0) {
                        if ($scope.fun > 0) {
                            console.log($scope.fun)
                        }
                    } else {
                        console.log($scope.customiseFun)
                    }
                }
            }
            console.log($scope.rolePermit, $scope.customisePermit, $scope.fun)
        }
    })

